﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.DataVisualization.Charting;

namespace BOMS.WebUI.Web.DataAnalyse
{
    public partial class GroupHistoryPic : System.Web.UI.Page
    {
       
        public byte Generation
        {
            get { return byte.Parse(Request.QueryString["generation"]); }
        }
        public string EquipID
        {
            get { return Request.QueryString["equipid"]; }
        }
        public string BatGrp
        {
            get { return Request.QueryString["batgrp"]; }
        }
        //public int BatteryID
        //{
        //    get { return int.Parse(Request.QueryString["batid"]); }
        //}
        public DateTime DtmBegin
        {
            get
            {
                if (!string.IsNullOrEmpty(Request.QueryString["dtmbegin"]))
                    return DateTime.Parse(Request.QueryString["dtmbegin"]);
                else
                    return new DateTime(2010, 1, 1);
            }
        }
        public DateTime DtmEnd
        {
            get
            {
                if (!string.IsNullOrEmpty(Request.QueryString["dtmend"]))
                    return DateTime.Parse(Request.QueryString["dtmend"]);
                else
                    return new DateTime(2050, 1, 1);
            }
        }
        public DateTime DtmRealBegin
        {
            get
            {
                if (ViewState["_DtmRealBegin"] == null)
                {
                    SetRealDtm2ViewState();
                }
                return (DateTime)ViewState["_DtmRealBegin"];
            }
        }
        public DateTime DtmRealEnd
        {
            get
            {
                if (ViewState["_DtmRealEnd"] == null)
                {
                    SetRealDtm2ViewState();
                }
                return (DateTime)ViewState["_DtmRealEnd"];
            }
        }
        void lnbChart_Click(object sender, EventArgs e)
        {
            LinkButton lnb = (LinkButton)sender;
            if (lnb.CommandName == "Preview")
            {
                hdfCurrentPageIndex.Value = (int.Parse(hdfCurrentPageIndex.Value) - 1).ToString();
            }
            else if (lnb.CommandName == "Nextview")
            {
                hdfCurrentPageIndex.Value = (int.Parse(hdfCurrentPageIndex.Value) + 1).ToString();
            }
            SingleBtn_Click(null, null);
        }
        //public int sel;
        protected void Page_Load(object sender, EventArgs e)
        {
            lnbBeForeChart.Click += new EventHandler(lnbChart_Click);
            lnbBehindChart.Click += new EventHandler(lnbChart_Click);
            if (!IsPostBack)
            {
                BLL.M_SubLans bll = new BLL.M_SubLans();
                Model.M_SubLans model = bll.GetModelByEquipMac(EquipID);
                lbIntro.Text = string.Format("按条件生成 现场主机:<b>{0}</b> 电池组:<b>{1}</b>  从 <b>{2}</b> 到 <b>{3}</b> 时间内的图形报表",
                    model.HostName,
                    BatGrp,                   
                    DtmRealBegin,
                    DtmRealEnd
                    );
            }
        }
        private void SetRealDtm2ViewState()
        {
            BLL.S_Warning bll = new BLL.S_Warning();
            DateTime?[] dtms = bll.GetRealLogTime(EquipID, BatGrp, -1);
            if (dtms[0] != null && dtms[0].HasValue)
            {
                ViewState["_DtmRealBegin"] = dtms[0].Value;
            }
            else
            {
                ViewState["_DtmRealBegin"] = new DateTime(2010, 1, 1);
            }
            if (dtms[1] != null && dtms[1].HasValue)
            {
                ViewState["_DtmRealEnd"] = dtms[1].Value;
            }
            else
            {
                ViewState["_DtmRealEnd"] = new DateTime(2050, 1, 1);
            }
        }
 
        private void ShopLnb(int PageCount, int CurPageIdx)
        {
            lnbBeForeChart.Visible = true;
            lnbBehindChart.Visible = true;


            int CurNum = CurPageIdx + 1;
            if (PageCount == 1)
            {
                lnbBeForeChart.Enabled = false;
                lnbBehindChart.Enabled = false;
            }
            else if (PageCount == 2)
            {
                if (CurNum == 1)
                {
                    lnbBeForeChart.Enabled = false;
                    lnbBehindChart.Enabled = true;
                }
                else if (CurNum == 2)
                {
                    lnbBeForeChart.Enabled = true;
                    lnbBehindChart.Enabled = false;
                }
            }
            else if (PageCount >= 3)
            {
                if (CurNum == 1)
                {
                    lnbBeForeChart.Enabled = false;
                    lnbBehindChart.Enabled = true;
                }
                else if (CurNum >= 2 && CurNum < PageCount)
                {
                    lnbBeForeChart.Enabled = true;
                    lnbBehindChart.Enabled = true;
                }
                else if (CurNum == PageCount)
                {
                    lnbBeForeChart.Enabled = true;
                    lnbBehindChart.Enabled = false;
                }
            }
            lbCurPageNO.Text = string.Format("({0}/{1})",CurPageIdx+1, PageCount);

        }
        protected void SingleBtn_Click(object sender, EventArgs e)
        {
            tblBtnImgs.Visible = false;


            BLL.BatteryAnaly ba = new BLL.BatteryAnaly();
            //电池主键（联合主键）（用Structure结构体类型传递）
            Model.BatteryKey key = new Model.BatteryKey();
            key.Generation = this.Generation;
            key.EquipID = this.EquipID;
            key.BatteryGrp = this.BatGrp;
            key.BatteryID = -1;//!!!!!!!!!!!!!(-1表示取电池组的数据)!!!!!!!!!!!!!!!!!!!!

            int pgsize = 0;
            int intv = 3;

            //快速扫描线图
            if (drpChartType.SelectedValue == "FastLine" || drpChartType.SelectedValue == "FastPoint")
            {
                intv = 3;
                pgsize = 500;
            }
            else
            {
                pgsize = int.Parse(tbxXPointCount.Text);
                TimeSpan ts = (DtmEnd > DtmRealEnd ? DtmRealEnd : DtmEnd) - (DtmBegin < DtmRealBegin ? DtmRealBegin : DtmBegin);
                double seconds = Math.Ceiling(ts.TotalSeconds);
                intv = Convert.ToInt32(Math.Ceiling(seconds / pgsize));
                if (intv < 3) { intv = 3; }//时间间隔不能小于3秒！
            }
            //查询和分页的几个参数（用Structure结构体类型传递）
            Model.AnalyParam parms = new Model.AnalyParam();
            parms.dtmBegin = this.DtmBegin;
            parms.dtmEnd = this.DtmEnd;
            parms.interv = intv;
            parms.pageSize = pgsize; //10;
            parms.curPage = int.Parse(hdfCurrentPageIndex.Value);


            int PageCount = 0;
            List<Model.BatteryAnaly> list = ba.GetBatteryAbstractData(key, parms, out PageCount);
            ShopLnb(PageCount, parms.curPage);//设置按钮状态


            
            //string dataJson="[";//[{country: "USA",visits: 4025}, {country: "China",visits: 1882}]
            //decimal value=0;
            //for (int i = 0; i < list.Count; i++)
            //{
            //    if (rbl.SelectedValue == "Vval")
            //        value=list[i].Vval;
            //    else if (rbl.SelectedValue == "Ival")
            //        value=list[i].Ival;
            //    else if (rbl.SelectedValue == "Rval")
            //        value=list[i].Rval;
            //    else if (rbl.SelectedValue == "Tval")
            //        value=list[i].Tval;
            //    else if (rbl.SelectedValue == "CapFree")
            //        value = list[i].CapFree;

            //    dataJson = dataJson + "{time:" + '"' + list[i].Time.ToString("yyyy-MM-dd") + '"' + ",value:" + '"' + value + '"' + "},";
            //}
            //dataJson = dataJson + "]";
            //this.JsonData1.Text = dataJson;
            ///////////////////////////////////////////////////////把分页隐藏掉
            this.lnbBeForeChart.Visible = false;
            this.lnbBehindChart.Visible = false;
            lbCurPageNO.Visible = false;

            System.Web.Script.Serialization.JavaScriptSerializer jsSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            this.JsonData1.Text = jsSerializer.Serialize(list);
        }


    }
}